﻿using System;
using System.Collections.Generic;
using HR60_PanelSoft.Model.BaseModel;

namespace HR60_PanelSoft.Model
{
    //GC: [g1]开合类负载[Pin]@s1
	/// <summary>
	/// 连接线路
	/// </summary>
    public abstract class Pin : RefModel
    {
		/// <summary>
		/// 负载类型
		/// </summary>
		public PinType type = PinType.NULL;

        /* [g1.s1]将序号属性提升到父类pin中 */
        /// <summary>
        /// 负载序号
        /// </summary>
        public int index = 1;

        /* end of [g1.s1] */

		/// <summary>
		/// 是否是连接pin
		/// </summary>
		public bool isMainPin = false;

		/// <summary>
		/// 已连接的设备,ref
		/// </summary>
		public string connectedDevice = null;

		/// <summary>
		/// 已连接的设备,ref to object
		/// </summary>
		/// <returns>The connected device.</returns>
		public Device getConnectedDevice()
		{
			return Device.find (connectedDevice);
		}

		/// <summary>
		/// 获得所在面板
		/// </summary>
		/// <returns>The panel.</returns>
		public Panel getPanel()
		{
			foreach (HR60_PanelSoft.Model.Net net in Proj.currentProj.nets) 
			{
				foreach (Panel panel in net.panels)
				{
					foreach (Pin pin in panel.loadPins)
					{
						if (pin.uuid == uuid)
							return panel;
					}
					if (panel.type.hasM485Model && panel.m485Pin.uuid == uuid)
						return panel;
				}
			}
			return null;
		}

		// find by uuid
		public static Pin find(string uuid)
		{
			foreach (HR60_PanelSoft.Model.Net net in Proj.currentProj.nets) 
			{
				foreach (Panel panel in net.panels)
				{
					foreach (Pin pin in panel.loadPins)
					{
						if (pin.uuid == uuid)
							return pin;
					}
					if (panel.type.hasM485Model && panel.m485Pin.uuid == uuid)
						return panel.m485Pin;
				}
			}
			return null;
		}

        public object[] getPanelAndNet()
        {
            object[] rst = new object[2];

            foreach (HR60_PanelSoft.Model.Net net in Proj.currentProj.nets)
            {
                foreach (Panel panel in net.panels)
                {
                    foreach (Pin pin in panel.loadPins)
                    {
                        if (pin.uuid == uuid)
                        {
                            rst[0] = panel;
                            rst[1] = net;
                            return rst;
                        }
                    }
                    if (panel.type.hasM485Model && panel.m485Pin.uuid == uuid)
                    {
                        rst[0] = panel;
                        rst[1] = net;
                        return rst;
                    }
                        
                }
            }
            return null;
        }
    }
}
